/**
 *  ByteCode.java : IceBird project
 *  6:56:39 
 */
package icebird.compiler.bytecode;

import icebird.metadata.ExceptionEntry;
import icebird.metadata.Method;
import icebird.metadata.constpool.ConstPool;

import java.nio.ByteBuffer;

/**
 * 
 * @author sergey
 */
public final class ByteCode {
	
	private Method method;
	
	private int maxLocals;
	private int maxStack;
	
	private ByteBuffer bytes;
	
	/**
	 * Create byte code buffer.
	 * @param source
	 */
	public ByteCode(Method source) {
		method = source;
		
		maxLocals = method.getCode().getMaxLocals();
		maxStack = method.getCode().getMaxStack();
		
		bytes = ByteBuffer.wrap(method.getCode().getCode());
	}
	
	/**
	 * Gets constant pool.
	 */
	public ConstPool getConstPool() {
		return method.getDeclaringType().getConstPool();
	}
	
	/**
	 * Gets code buffer.
	 * @return ByteBuffer
	 */
	public ByteBuffer getBytes() {
		return bytes;
	}

	/**
	 * @return the maxLocals
	 */
	public final int getMaxLocals() {
		return maxLocals;
	}

	/**
	 * @return the maxStack
	 */
	public final int getMaxStack() {
		return maxStack;
	}

	/**
	 * @return the method
	 */
	public final Method getMethod() {
		return method;
	}
	
	public final int getLength() {
		return bytes.limit();
	}
	
	public final ExceptionEntry[] getExceptions() {
		return method.getCode().getExceptions();
	}
}
